<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of signup
 *
 * @author Administrator
 */
class Signup extends CI_Controller {

  public function __construct() {
    parent::__construct();
  }

  public function index() {
    $countries = $this->country_model->get_all();
    $this->load->view('provider/signup', array('countries' => $countries,
        'captcha' => generate_captcha(334),
        'title_html' => $this->lang->line('label.provider.title.signup')));
  }

  public function process() {
    $user = $_REQUEST;
    $group = $this->group_model->get(array('name' => 'Provider'));
    if (!empty($group)) {
      $user['group_id'] = $group['id'];
    } else
      $user['group_id'] = 0;
    $user = $this->user_model->create_obj($user);
    $provider = $this->provider_user_model->create_obj($_REQUEST);
    $errors = array_merge($this->user_model->validate($user), $this->provider_user_model->validate($provider));
    if (!empty($errors))
      set_flash_error($errors);
    else {
      $user['salt'] = $this->auth->generateActiveCode(10);
      $user['password'] = $user['confirm_pass'] = md5($user['salt'] . md5($user['password']));
      if ($this->setting_model->item('enviroment') == EVR_TEST) {
        $user['is_active'] = true;
        $user['active_code'] = '';
      } else {
        $user['is_active'] = false;
        $user['active_code'] = $this->auth->generateActiveCode(50) . uniqid();
      }
      $user['created_on'] = time();
      if (!empty($user['first_name']) || !empty($user['last_name'])) {
        $user['display_name'] = trim($user['first_name'] . ' ' . $user['last_name']);
      } else {
        $user['display_name'] = $user['username'];
      }
      $user_id = $this->user_model->save($user);
      if (is_integer($user_id)) {
        $avatar = $this->base_lib->_upload_img('avatar', 'provider_user');
        $provider['avatar'] = $avatar;
        $provider['user_id'] = $user_id;
        $provider['date_create'] = time();
        if ($this->setting_model->item('enviroment') == 1) {
          $provider['status'] = PROFILE_STATUS_ACTIVE;
        } else {
          $provider['status'] = PROFILE_STATUS_UNACTICE;
        }
        $provider_id = $this->provider_user_model->save($provider);
        if (is_integer($provider_id)) {
          if ($this->setting_model->item('require_active_via_email') && $user['is_active'] == false) {
            $this->load->library('provider_user_lib');
            $this->provider_user_lib->email_active_account(array_merge($user, $provider, array('link' => site_url('provider/profile/active/' . $user['active_code']))));
          }
          set_flash_message($this->lang->line('message.provider.created', array($provider_id)));
          redirect('provider/index');
          return;
        } else {
          $this->user_model->delete($user_id);
          set_flash_error($this->lang->line('error.faltal.error'));
        }
      } else
        set_flash_error($this->lang->line('error.faltal.error'));
    }
    $this->load->view('provider/signup', array('obj' => $_REQUEST, 'captcha' => generate_captcha(334),
        'title_html' => $this->lang->line('label.provider.title.signup')));
  }

}